package com.asset.salesforce.view;

import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.asset.salesforce.view.HeaderAndFooter;
import com.asset.salesforce.domain.*;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PdfView extends AbstractPdfView{

	
	public static Font normalFont = new Font(FontFactory.getFont(String.valueOf(Font.TIMES_ROMAN),BaseFont.TIMES_ROMAN,9,Font.NORMAL));
     
	@SuppressWarnings({ "unchecked" })
	@Override
	protected void buildPdfDocument(Map<String, Object> model,
			Document document, PdfWriter writer, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		document.setPageSize(PageSize.A4);
		writer.setPageEvent(new HeaderAndFooter());
		document.open();
	      document.addAuthor("Hisham Mounir");
	      document.addCreator("Asset Security");
	      document.addSubject("SalesForce CRM STAFF");
	      document.addCreationDate();
	      document.addTitle("Invoice");
	      //Font Settings
	      int fontSize = 10;
		  String fontName = "arial.ttf";
	      ClassLoader fontcl = PdfView.class.getClassLoader();
	      URL extUrl = fontcl.getResource(fontName);
	      String fontPath = extUrl.toString();
	      Font Arial = FontFactory.getFont(fontPath,BaseFont.IDENTITY_H, fontSize);
	      //////////////////////////////////////////////////////////////////////////////////
	      //Logo
	      String imagePath = "assetlogo.png";
	      ClassLoader imagecl = PdfView.class.getClassLoader();
	      URL imgURL = imagecl.getResource(imagePath);	      
	      Image image = Image.getInstance(imgURL);
	      image.scalePercent(30);
	      document.add(image);
	      ////////////////////////////////////////////////////////////////////////////////////
	      //Data List
	      //List<InvoiceSku> invoiceSkus = (List<InvoiceSku>) model.get("invoiceSkus");
	      List<InvoiceSku> invoiceSkus = (List<InvoiceSku>) model.get("invoiceSkus"); 
	      List<InvoiceService> invoiceServices = (List<InvoiceService>) model.get("invoiceServices");
	      Invoice invoice = (Invoice) model.get("invoice");
	      //Contact contact = (Contact) model.get("contact");
	      Account account = (Account) model.get("account");
	      Lead lead = (Lead) model.get("lead");
	      //////////////////////////////////////////////////////////////////////////////////////	      
	      PdfPTable priceShowTable = new PdfPTable(1);
	      priceShowTable.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
	      priceShowTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
	      priceShowTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
	      priceShowTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
	      PdfPCell priceShowCell;     	
	      Paragraph arabPrice =  new Paragraph("عرض اسعار",FontFactory.getFont(fontPath,BaseFont.IDENTITY_H, 12));
	      arabPrice.setAlignment(Element.ALIGN_CENTER);
	      priceShowCell = new PdfPCell(arabPrice);
	      priceShowCell.setBorder(Rectangle.NO_BORDER);
	      priceShowCell.setHorizontalAlignment(Element.ALIGN_CENTER);
	      priceShowTable.addCell(priceShowCell);
	      document.add(priceShowTable);
	      ///////////////////////////////////////////////////////////////////////////////////////////////////////////
	      //Quatation Paragraph
	      	Paragraph quatation = new Paragraph("Quotation",FontFactory.getFont(String.valueOf(Font.TIMES_ROMAN),BaseFont.TIMES_ROMAN,20,Font.BOLD));
	      	quatation.setAlignment(Element.ALIGN_CENTER);
	      	document.add(quatation);
	      	/////////////////////////////////////////////////////////
	      	//Form Table
	      	FormTable formTableFinal = new FormTable();
			document.add(formTableFinal.pdfPTable(invoice, account, lead));
			//////////////////////////////////////////////////////////////////////////
			//Invoice Product Table
			InvoiceProductTable invoiceProductTable = new InvoiceProductTable();
			document.add(invoiceProductTable.pdfPTable(invoiceSkus));
			////////////////////////////////////////////////////////////////////////
			//Invoice Serivce Table
			InvoiceServiceTable invoiceServiceTable = new InvoiceServiceTable();
			document.add(invoiceServiceTable.pdfPTable(invoiceServices));
			/////////////////////////////////////////////////////////////////////////
			//Total Table
			TotalTable totalTable = new TotalTable();
			document.add(totalTable.pdfPTable(invoice));
			///////////////////////////////////////////////////////////////////////
			PdfPTable spaceTable = new PdfPTable(1);
			spaceTable.setWidthPercentage(100);
			spaceTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);
			PdfPCell spaceCell;
			if(invoiceSkus.size()==0){
				spaceCell = new PdfPCell(new Phrase("", Arial));
				spaceCell.setBorder(Rectangle.NO_BORDER);
				spaceTable.addCell(spaceCell);
				spaceTable.setSpacingBefore(60f);
				document.add(spaceTable);
			}
			
			///////////////////////////////////////////////////////////////////////
			//Agreement Table
			AgreementTable agreementTable = new AgreementTable();
			document.add(agreementTable.pdfPTable());
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//Client Signtature
			PdfPTable clientSigintureTableOne = new PdfPTable(1);
			clientSigintureTableOne.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
			clientSigintureTableOne.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            PdfPCell clientSiginturecellOne;
            ///////////////////////////////////////////////////////////////////////////
            //Client Signiture Cell
            clientSiginturecellOne = new PdfPCell(new Phrase("توقيع العميل :", Arial));
            clientSiginturecellOne.setBorder(Rectangle.NO_BORDER);
            clientSiginturecellOne.setHorizontalAlignment(Element.ALIGN_LEFT);
            clientSigintureTableOne.addCell(clientSiginturecellOne);
            ///////////////////////////////////////////////////////////////////////////
            //Client Date Cell
            clientSiginturecellOne = new PdfPCell(new Phrase("التاريخ :", Arial));
            clientSiginturecellOne.setHorizontalAlignment(Element.ALIGN_LEFT);
            clientSiginturecellOne.setBorder(Rectangle.NO_BORDER);
            clientSigintureTableOne.addCell(clientSiginturecellOne);
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //Dot Table
            PdfPTable clientSigintureTableTwo = new PdfPTable(1);
            clientSigintureTableTwo.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            clientSigintureTableTwo.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            PdfPCell clientSiginturecellTwo;
            clientSiginturecellTwo = new PdfPCell(new Phrase("........................................................", Arial));
            clientSiginturecellTwo.setBorder(Rectangle.NO_BORDER);
            clientSiginturecellTwo.setHorizontalAlignment(Element.ALIGN_LEFT);
            clientSigintureTableTwo.addCell(clientSiginturecellTwo);
            clientSiginturecellTwo = new PdfPCell(new Phrase("........................................................", Arial));
            clientSiginturecellTwo.setBorder(Rectangle.NO_BORDER);
            clientSiginturecellTwo.setHorizontalAlignment(Element.ALIGN_LEFT);
            clientSigintureTableTwo.addCell(clientSiginturecellTwo);
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //Client Table Final
            PdfPTable clientTableFinal = new PdfPTable(2);
            clientTableFinal.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            clientTableFinal.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            clientTableFinal.setHorizontalAlignment(Element.ALIGN_RIGHT);
            //table4.setWidthPercentage(100);
            PdfPCell clientCellFinal;
            clientCellFinal = new PdfPCell(clientSigintureTableOne);
            clientCellFinal.setBorder(Rectangle.NO_BORDER);
            clientTableFinal.addCell(clientCellFinal);
            clientCellFinal = new PdfPCell(clientSigintureTableTwo);
            clientCellFinal.setBorder(Rectangle.NO_BORDER);
            clientTableFinal.addCell(clientCellFinal);
            float[] clientcolumnWidths = new float[] {80f,20f};
            clientTableFinal.setWidths(clientcolumnWidths);
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//Company Signtature
			PdfPTable companySigintureTableOne = new PdfPTable(1);
			companySigintureTableOne.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
			companySigintureTableOne.getDefaultCell().setBorder(Rectangle.NO_BORDER);
			PdfPCell companySiginturecellOne;
			///////////////////////////////////////////////////////////////////////////
			//Client Signiture Cell
			companySiginturecellOne = new PdfPCell(new Phrase("توقيع العميل :", Arial));
			companySiginturecellOne.setBorder(Rectangle.NO_BORDER);
			companySiginturecellOne.setHorizontalAlignment(Element.ALIGN_LEFT);
			companySigintureTableOne.addCell(companySiginturecellOne);
			///////////////////////////////////////////////////////////////////////////
			//Client Date Cell
			companySiginturecellOne = new PdfPCell(new Phrase("التاريخ :", Arial));
			companySiginturecellOne.setHorizontalAlignment(Element.ALIGN_LEFT);
			companySiginturecellOne.setBorder(Rectangle.NO_BORDER);
			companySigintureTableOne.addCell(companySiginturecellOne);
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//Dot Table
			PdfPTable companySigintureTableTwo = new PdfPTable(1);
			companySigintureTableTwo.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
			companySigintureTableTwo.getDefaultCell().setBorder(Rectangle.NO_BORDER);
			PdfPCell companySiginturecellTwo;
			companySiginturecellTwo = new PdfPCell(new Phrase("........................................................", Arial));
			companySiginturecellTwo.setBorder(Rectangle.NO_BORDER);
			companySiginturecellTwo.setHorizontalAlignment(Element.ALIGN_LEFT);
			companySigintureTableTwo.addCell(companySiginturecellTwo);
			companySiginturecellTwo = new PdfPCell(new Phrase("........................................................", Arial));
			companySiginturecellTwo.setBorder(Rectangle.NO_BORDER);
			companySiginturecellTwo.setHorizontalAlignment(Element.ALIGN_LEFT);
			companySigintureTableTwo.addCell(companySiginturecellTwo);
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            //Company Table Final
			PdfPTable companyTableFinal = new PdfPTable(2);
			companyTableFinal.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
			companyTableFinal.getDefaultCell().setBorder(Rectangle.NO_BORDER);
			companyTableFinal.setHorizontalAlignment(Element.ALIGN_RIGHT);
          //table4.setWidthPercentage(100);
			PdfPCell companyCellFinal;
			companyCellFinal = new PdfPCell(companySigintureTableOne);
			companyCellFinal.setBorder(Rectangle.NO_BORDER);
			companyTableFinal.addCell(companyCellFinal);
			companyCellFinal = new PdfPCell(companySigintureTableTwo);
			companyCellFinal.setBorder(Rectangle.NO_BORDER);
			companyTableFinal.addCell(companyCellFinal);
			float[] companycolumnWidths = new float[] {80f,20f};
			companyTableFinal.setWidths(companycolumnWidths);
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////
            PdfPTable table4 = new PdfPTable(2);
            table4.setWidthPercentage(100);
            table4.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
            table4.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            table4.setHorizontalAlignment(Element.ALIGN_RIGHT);
            //table4.setWidthPercentage(100);
            PdfPCell cell4;
            cell4 = new PdfPCell(clientTableFinal);
            cell4.setBorder(Rectangle.NO_BORDER);
            table4.addCell(cell4);
            cell4 = new PdfPCell(companyTableFinal);
            cell4.setBorder(Rectangle.NO_BORDER);
            table4.addCell(cell4);
            float[] arabcolumnWidths = new float[] {60f,60f};
            table4.setWidths(arabcolumnWidths);
            table4.setSpacingAfter(10f);
            
//			document.add(table3);
			document.add(table4);
	      document.close();
	      System.out.println("Done");
	}
}